#ifndef __STRLIST_H__
#define __STRLIST_H__

namespace inf {

    /*
    ===============================================================================

    TStrList

    ===============================================================================
    */
    template <typename StrType>
    int StrSortCompare( const StrType* a, const StrType* b ) {
        return a->cmpn(b->c_str(), b->length(), true);
    }

    template <typename Type>
    class TStrList : public ListGranularityOne<TStr<Type> > {
    public:
        TStrList( int gran = 1 ) : ListGranularityOne<TStr<Type> >::ListGranularityOne( gran ) { }
        void    sort( typename List<TStr<Type> >::cmp_t* compare = StrSortCompare );
        size_t  total_size( void ) const;

    };

    /*
    ================
    TStrList::sort

    Sorts the list of strings alphabetically. Creates a list of pointers to the actual strings and sorts the
    pointer list. Then copies the strings into another list using the ordered list of pointers.
    ================
    */
    template <typename Type>
    void TStrList<Type>::sort( typename List<TStr<Type> >::cmp_t *compare ) {
        int i;

        if ( !this->num ) {
            return;
        }

        List<TStr<Type> >		other;
        List<TStr<Type>*>	pointerList;

        pointerList.ready_num( this->num );
        for( i = 0; i < this->num; i++ ) {
            pointerList[ i ] = &( *this )[ i ];
        }

        pointerList.sort();

        other.ready_num( this->num );
        other.set_granularity( this->granularity );
        for( i = 0; i < other.size(); i++ ) {
            other[ i ] = *pointerList[ i ];
        }

        other.sort(compare);

        this->swap( other );
    }

    /*
    ================
    TStrList::total_size
    ================
    */
    template <typename Type>
    size_t TStrList<Type>::total_size( void ) const {
        size_t s;
        int i;

        s = sizeof( *this );
        for( i = 0; i < this->size(); i++ ) {
            s += ( *this )[ i ].total_size();
        }

        return s;
    }

    typedef TStrList<char>      StrList;
    typedef TStrList<wchar_t>   WStrList;

}   // ::inf

#endif /* !__STRLIST_H__ */
